package com.ai.app.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/chat")
@RestController
public class ChatClientController {
    @Autowired
    private ChatClient chatClient;

       @GetMapping("/call")
         public String generation(String userInput) {
           System.out.println("ChatClientController 已进入方法"); // 验证是否被实例化
           return this.chatClient.prompt()
                //用户输入
                 .user(userInput)
                 //调用AI
                 .call()
                 //返回响应
                 .content();
    }
    //定义一个实体类，定义一个菜单名称
    record Recipe(String dish, List<String> ingredients){

    }
    @RequestMapping("/entity")
    public String entity(String userInput){
       Recipe recipe=chatClient.prompt()
                   .user(String.format("请帮我生成%s的食谱",userInput))
                   .call()
                   .entity(Recipe.class);
       return recipe.toString();
    }

//完成流式输出
    @GetMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput) {
        System.out.println("ChatClientController 已进入方法"); // 验证是否被实例化
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
              //  .advisors(new SimpleLoggerAdvisor())      只对方法
                //调用AI,进行流式输出
                .stream()
                //返回响应
                .content();
    }
}
